package edu.office.applicationRepository;


import edu.office.entities.Parent;
import edu.office.entities.School;
import edu.office.tools.EOMessage;
import edu.office.tools.EOMessage1;
import edu.office.tools.StaticFields;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;

import java.util.LinkedList;
import java.util.List;

@Named("parentRepositoryA")
@ApplicationScoped
public class ParentRepository extends ApplicationCommonRepository<Parent> {
    private Parent nullParent;

    public Parent getNullParent() {
        if (null == nullParent) {
           nullParent = find("select * from parent where name='PNULL'").firstResult();
        }
        return nullParent;
    }
    public List<Parent> search(String searchName, School school) {
        List<Parent> searchedParentList = new LinkedList<>();
        if (null != searchName && searchName.trim().length() > 0) {
            String schoolCondstring = "", searchCondString = "";
            if (school != null) {
                schoolCondstring = " school_id=" + school.getID();
            }
            if (null != searchName && searchName.trim().length() > 0) {
                searchCondString = " locate('" + searchName.trim() + "',name)>0 or "
                        + " locate('" + searchName.trim().toLowerCase() + "',LOWER(FIRSTNAME))>0 or "
                        + " locate('" + searchName.trim().toLowerCase() + "',LOWER(SECONDNAME))>0 or "
                        + " locate('" + searchName.trim().toLowerCase() + "',LOWER(EMAIL))>0 or "
                        + " locate('" + searchName.trim().toLowerCase() + "',LOWER(PHONE))>0";
            }
            //================compose cases
            int con = 0;
            if (schoolCondstring.trim().length() == 0 && searchCondString.trim().length() == 0) {
                con = 0;//nothing to search
            } else {
                if (schoolCondstring.trim().length() > 0 & searchCondString.trim().length() == 0) {
                    con = 1;//Only school condition
                } else {
                    if (schoolCondstring.trim().length() == 0 & searchCondString.trim().length() > 0) {
                        con = 2;//Only name conditon
                    } else {
                        con = 3;//both condition
                    }
                }
            }

            //===================conditionString===============
            String condtionString = "";
            switch (con) {
                case 0:
                    break;
                case 1:
                    condtionString = schoolCondstring;
                    break;
                case 2:
                    condtionString = searchCondString;
                    break;
                case 3:
                    condtionString = schoolCondstring + " and " + searchCondString;
                    break;
            }
            if (condtionString.trim().length() > 0) {
                searchedParentList.clear();
                List<Parent> tem = find("select * from parent where " + condtionString).list();
                tem.forEach(e -> {
                    searchedParentList.add((Parent) e);
                });
            }
        }
        return searchedParentList;
    }

    public String mycreate(Parent parent) {
        //1. check whether the name existed? If true, deny the operation;else continue;
        if (find("NAME", parent.getNAME()).list().isEmpty()) {
            parent.setPASSWORD(StaticFields.encrypt(parent.getPASSWORD()));
            create(parent);
            return EOMessage.getEOMessageENUM(EOMessage1.Succeed);

        } else {
            return EOMessage.getEOMessageENUM(EOMessage1.Already_Exist);
        }

    }
}
